package com.mis.clothespos.inventory.controller;

import com.mis.clothespos.clothes.domain.clothes;
import com.mis.clothespos.clothes.service.IclothesService;
import com.mis.clothespos.common.AjaxResult;
import com.mis.clothespos.common.BaseController;
import com.mis.clothespos.in.domain.In;
import com.mis.clothespos.inventory.domain.*;
import com.mis.clothespos.inventory.service.InventoryService;
import com.mis.clothespos.sale.service.saleService;
import com.mis.clothespos.shiro.common.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import static com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmIndexes.UUID;

@Controller
@RequestMapping("/inventory")
public class InventoryController extends BaseController{

    @Autowired
    InventoryService inventoryService;
    @Autowired
    IclothesService c_service;


    @RequestMapping(value ="/addInventory")
    @ResponseBody
    public AjaxResult addInventory(@RequestBody VueInventory vueinventory){
        //提取vue前端传来的类
        List<InventoryItem> item=vueinventory.getItem();
        Inventory inventory=new Inventory();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String inventoryId=df2.format(new Date());
        Date inventoryTime=new Date();
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        inventory.setAuditorname(userName);
        if(userName==null){
            inventory.setAuditorname("admin");
        }
        inventory.setId(inventoryId);
        inventory.setTime(inventoryTime);
        //inventory.setAuditorid(ShiroUtils.getSysUser());
        //inventory.setCteatby(ShiroUtils.getSysUser());
        //插入盘点类
        inventoryService.insertInventory(inventory);

        for(int i=0;i<item.size();i++){
            InventoryItem inventoryitem=item.get(i);
            String productId=inventoryitem.getId();
            inventoryitem.setId(RandomStringUtils.randomAlphanumeric(10));

            //插入订单条目
            inventoryService.insertInventoryItem(inventoryitem);
            InventoryToItem inventoryToItem=new InventoryToItem();
            inventoryToItem.setId(RandomStringUtils.randomAlphanumeric(9));
            inventoryToItem.setInventoryid(inventory.getId());
            inventoryToItem.setInventoryitemid(inventoryitem.getId());
            //关联中间表
            inventoryService.insertInventoryToItem(inventoryToItem);
        }
        AjaxResult result =new AjaxResult();
        result.put("code",200);
        result.put("msg","新增盘点单成功");
        return  result;
    }

    @PostMapping("/deleteInventoryById")
    @ResponseBody
    public AjaxResult deleteInventoryById(@RequestBody Inventory inventory) {
        inventoryService.deleteByPrimaryKey(inventory.getId());
        AjaxResult result =new AjaxResult();
        result.put("code", 200);
        result.put("message", "查询成功");
        return  result;
    }

    //@RequiresPermissions("user:view")
    @GetMapping("/selectInventoryClothes")
    @ResponseBody
    public AjaxResult selectInventoryClothes(clothes c){
        AjaxResult result = new AjaxResult();
        List<InventoryItem> inventoryItem = new ArrayList<InventoryItem>();
        List<clothes> clothes=c_service.getClothes(c);
        for(int i = 0 ; i < clothes.size() ; i++) {
            InventoryItem inv = new InventoryItem();
            inv.setId(clothes.get(i).getId().toString());
            //inventoryItem.get(i).setId(clothes.get(i).getId().toString());
            inv.setDesciption(clothes.get(i).getDesciption());
            inv.setColortype(clothes.get(i).getColortype());
            inv.setSizetype(clothes.get(i).getSizetype());
            inv.setPrice(clothes.get(i).getPrice());
            inv.setStylename(clothes.get(i).getStylename());
            inv.setName(clothes.get(i).getName());
            inv.setAccountnumber(clothes.get(i).getNumber());
            inventoryItem.add(i,inv);
        }
        result.put("inventoryitem",inventoryItem);
        result.put("totalII",inventoryItem.size());

        return result;

    }

    //@RequiresPermissions("user:view")
    @GetMapping("/selectInventory")
    @ResponseBody
    public AjaxResult selectInventory(){
        AjaxResult result = new AjaxResult();
        List<Inventory> inventory = inventoryService.selectInventory();
        //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String inventoryId=df.format(new Date());
        //for(int i=0;i<inventory.size();i++){
        //    System.out.println(inventory.get(i).getTime());
        //}
        result.put("inventory",inventory);
        result.put("totalI",inventory.size());
        return result;

    }

    //@RequiresPermissions("user:view")
    @GetMapping("/selectInventoryItemById")
    @ResponseBody
    public AjaxResult selectInventoryItemById(String id){
        List<InventoryItem> inventoryItemById = inventoryService.selectInventoryItemById(id);
        List<InventoryItemResult> inventoryItemByIdResult = new ArrayList<InventoryItemResult>();
        for(int i = 0 ; i < inventoryItemById.size() ; i++) {
            InventoryItemResult invr = new InventoryItemResult();
            invr.setId(inventoryItemById.get(i).getId());
            invr.setDesciption(inventoryItemById.get(i).getDesciption());
            invr.setColortype(inventoryItemById.get(i).getColortype());
            invr.setSizetype(inventoryItemById.get(i).getSizetype());
            invr.setPrice(inventoryItemById.get(i).getPrice());
            invr.setStylename(inventoryItemById.get(i).getStylename());
            invr.setName(inventoryItemById.get(i).getName());
            invr.setAccountnumber(inventoryItemById.get(i).getAccountnumber());
            invr.setRealnumber(inventoryItemById.get(i).getRealnumber());
            long an = inventoryItemById.get(i).getAccountnumber().longValue();
            long rn = inventoryItemById.get(i).getRealnumber().longValue();
            long pr = inventoryItemById.get(i).getPrice().longValue();
            long ml = (an-rn)*pr;
            Long moneyloss = new Long(ml);
            invr.setMoneyloss(moneyloss);
            inventoryItemByIdResult.add(i,invr);
            //System.out.println(invr.getMoneyloss());
        }
        AjaxResult result = new AjaxResult();
        result.put("inventoryitembyid",inventoryItemByIdResult);
        result.put("totalIIB",inventoryItemByIdResult.size());
        return result;

    }
}
